package com.haitun.risk.govern.service.impl;


import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.haitun.risk.govern.commons.BaseConsts;
import com.haitun.risk.govern.dao.ReceiveInfoDao;
import com.haitun.risk.govern.model.AddressRestrict;
import com.haitun.risk.govern.model.CertificateRestrict;
import com.haitun.risk.govern.model.PhoneRestrict;
import com.haitun.risk.govern.model.ReceiveInfo;
import com.haitun.risk.govern.model.RiskOrdersInfo;
import com.haitun.risk.govern.service.ReceiveInfoService;
import com.haitun.risk.govern.vo.ErpOrdersInfoRequestVO;
import com.haitun.risk.govern.vo.ResponseInfoVO;



@Service
public class ReceiveInfoServiceImpl implements ReceiveInfoService {

	
	@Autowired
	private ReceiveInfoDao receiveInfoDao;

	@Override
	public List<ReceiveInfo> findByCertificateAndAddress(String certificate, String address) {
		ReceiveInfo receiveInfo=new ReceiveInfo();
		receiveInfo.setCertificate(certificate);
		receiveInfo.setAddress(address);
		return receiveInfoDao.findByReceiveInfo(receiveInfo);
	}

	@Override
	public List<ReceiveInfo> findByPhone(String phone,int currentYear) {
		ReceiveInfo receiveInfo=new ReceiveInfo();
		receiveInfo.setPhone(phone);
		receiveInfo.setCurrentYear(currentYear);
		return receiveInfoDao.findByReceiveInfo(receiveInfo);
	}

	@Override
	public List<ReceiveInfo> findByPhoneAndAddress(String phone, String address) {
		ReceiveInfo receiveInfo=new ReceiveInfo();
		receiveInfo.setPhone(phone);
		receiveInfo.setAddress(address);
		return receiveInfoDao.findByReceiveInfo(receiveInfo);
	}

	@Override
	public List<ReceiveInfo> findByCertificate(String certificate,int currentYear) {
		ReceiveInfo receiveInfo=new ReceiveInfo();
		receiveInfo.setCertificate(certificate);
		receiveInfo.setCurrentYear(currentYear);
		return receiveInfoDao.findByReceiveInfo(receiveInfo);
	}

	@Override
	public ResponseInfoVO checkRiskRank(ErpOrdersInfoRequestVO erpOrdersInfoRequestVO) {
		ResponseInfoVO responseInfoVO=new ResponseInfoVO();
		Map<String,Object> detailInfo = new HashMap<>();
		int riskRank=BaseConsts.RISKRANK_LOW;
		StringBuffer riskItemSb=new StringBuffer("");
		RiskOrdersInfo riskOrdersInfo=erpOrdersInfoRequestVO.getRiskOrdersInfo();
		
		int currentYear = Calendar.getInstance().get(Calendar.YEAR);
		//1、查询当前买家身份证、手机号、收货地址今年累计额度
		//校验身份证额度信息
		CertificateRestrict certificateRestrict=receiveInfoDao.getByCertificateAndYear(new CertificateRestrict(riskOrdersInfo.getCertificate(),currentYear));
		if(certificateRestrict!=null){
			int amountTotal=riskOrdersInfo.getOrdersTotal()+certificateRestrict.getAmount();
			//307、该买家身份证号购买额度已经超过20000元
			if(amountTotal>20000){
				riskRank=BaseConsts.RISKRANK_HIGH;
				detailInfo.put("riskRank", riskRank);
				riskItemSb.append(BaseConsts.RISKITEM_307).append(",");
			//
			}else if(amountTotal>=18000&&amountTotal<=20000){
				//到达18000元
				System.out.println("到达18000元");
			}
		}
		//校验手机号额度信息
		PhoneRestrict phoneRestrict=receiveInfoDao.getByPhoneAndYear(new PhoneRestrict(riskOrdersInfo.getBuyerPhone(),currentYear));
		if(phoneRestrict!=null){
			int amountTotal=riskOrdersInfo.getOrdersTotal()+phoneRestrict.getAmount();
			//308、该买家身份证号购买额度已经超过20000元
			if(amountTotal>20000){
				riskRank=BaseConsts.RISKRANK_HIGH;
				detailInfo.put("riskRank", riskRank);
				riskItemSb.append(BaseConsts.RISKITEM_308).append(",");
			//
			}else if(amountTotal>=18000&&amountTotal<=20000){
				//到达18000元
				System.out.println("到达18000元");
			}
		}
		//校验收货地址额度信息
		AddressRestrict addressRestrict=receiveInfoDao.getByAddressAndYear(new AddressRestrict(riskOrdersInfo.getAddress(),currentYear));
		if(addressRestrict!=null){
			int amountTotal=riskOrdersInfo.getOrdersTotal()+addressRestrict.getAmount();
			//309、该买家身份证号购买额度已经超过20000元
			if(amountTotal>20000){
				riskRank=BaseConsts.RISKRANK_HIGH;
				detailInfo.put("riskRank", riskRank);
				riskItemSb.append(BaseConsts.RISKITEM_309).append(",");
			//
			}else if(amountTotal>=18000&&amountTotal<=20000){
				//到达18000元
				System.out.println("到达18000元");
			}
		}
		
		//1、根据收货人查询收货地址或手机号的个数
		List<ReceiveInfo> receiveInfoList1=findByCertificate(riskOrdersInfo.getCertificate(),currentYear);
		Map <String,Integer> certificateAddressMap = new HashMap <String,Integer>();
		Map <String,Integer> certificatePhoneMap = new HashMap <String,Integer>();
		String currentCertificateAddress=riskOrdersInfo.getCertificate()+"-"+riskOrdersInfo.getAddress();
		//检测查询出的结果是否有同身份证-地址，且同手机号的
		boolean currentCertificatePhoneflag=false;
		String currentCertificatePhone=riskOrdersInfo.getCertificate()+"-"+riskOrdersInfo.getBuyerPhone();
		//检测查询出的结果是否有同身份证-手机号，且同地址的
		boolean currentCertificateAddressflag=false;
		if(!receiveInfoList1.isEmpty()&&receiveInfoList1.size()>=1){
			for (ReceiveInfo receiveInfo : receiveInfoList1) {
				String certificateAddress=receiveInfo.getCertificate()+"-"+receiveInfo.getAddress();
				String certificatePhone=receiveInfo.getCertificate()+"-"+receiveInfo.getPhone();
				certificateAddressMap.put(certificateAddress, certificateAddressMap.get(certificateAddress)==null?1:(certificateAddressMap.get(certificateAddress)+1));
				certificatePhoneMap.put(certificatePhone, certificatePhoneMap.get(certificatePhone)==null?1:(certificatePhoneMap.get(certificatePhone)+1));
				
				if(certificateAddress.equals(currentCertificateAddress)){
					//存在与当前用户同身份证-地址，且同手机号的
					if(receiveInfo.getPhone().equals(riskOrdersInfo.getBuyerPhone())){
						currentCertificatePhoneflag=true;
					}
				}
				
				if(certificatePhone.equals(currentCertificatePhone)){
					//存在与当前用户同身份证-手机号，且同地址的
					if(receiveInfo.getAddress().equals(riskOrdersInfo.getAddress())){
						currentCertificateAddressflag=true;
					}
				}
				
			}
			
			//判断加入当前手机号信息
			if(!currentCertificatePhoneflag){
				if(certificateAddressMap.get(currentCertificateAddress)==null){
					certificateAddressMap.put(currentCertificateAddress, 1);
				}else{
					certificateAddressMap.put(currentCertificateAddress, certificateAddressMap.get(currentCertificateAddress)+1);
				}
			}
			//判断加入当前地址信息
			if(!currentCertificateAddressflag){
				if(certificatePhoneMap.get(currentCertificatePhone)==null){
					certificatePhoneMap.put(currentCertificatePhone, 1);
				}else{
					certificatePhoneMap.put(currentCertificatePhone, certificatePhoneMap.get(currentCertificatePhone)+1);
				}
			}
			//同一收货人，同一收货地址，大于3个手机号---301
			if(!certificateAddressMap.isEmpty()&&certificateAddressMap.get(currentCertificateAddress)>3){
				riskRank=BaseConsts.RISKRANK_HIGH;
				detailInfo.put("riskRank", riskRank);
				riskItemSb.append(BaseConsts.RISKITEM_301).append(",");
			}
			
			//同一收货人，大于等于2个收货地址---302
			if(!certificateAddressMap.isEmpty()&&certificateAddressMap.size()>=2){
				riskRank=BaseConsts.RISKRANK_HIGH;
				detailInfo.put("riskRank", riskRank);
				riskItemSb.append(BaseConsts.RISKITEM_302).append(",");
			}
			
			//同一收货人，大于等于2个手机号---303
			if(!certificatePhoneMap.isEmpty()&&certificatePhoneMap.size()>=2){
				riskRank=BaseConsts.RISKRANK_HIGH;
				detailInfo.put("riskRank", riskRank);
				riskItemSb.append(BaseConsts.RISKITEM_303).append(",");
			}
			
		}
		
		
		//2、根据手机号查询收货地址或收货人的个数
		List<ReceiveInfo> receiveInfoList2=findByPhone(riskOrdersInfo.getBuyerPhone(),currentYear);
		Map <String,Integer> phoneAddressMap = new HashMap <String,Integer>();
		Map <String,Integer> phoneCertificateMap = new HashMap <String,Integer>();
		String currentPhoneAddress=riskOrdersInfo.getBuyerPhone()+"-"+riskOrdersInfo.getAddress();
		//检测查询出的结果是否有同手机号-地址，且同身份证的
		boolean currentPhoneCertificateflag=false;
		String currentPhoneCertificate=riskOrdersInfo.getBuyerPhone()+"-"+riskOrdersInfo.getCertificate();
		//检测查询出的结果是否有同手机号-身份证，且同地址的
		boolean currentPhoneAddressflag=false;
		if(!receiveInfoList2.isEmpty()&&receiveInfoList2.size()>=2){
			for (ReceiveInfo receiveInfo : receiveInfoList2) {
				String phoneAddress=receiveInfo.getPhone()+"-"+receiveInfo.getAddress();
				String phoneCertificate=receiveInfo.getPhone()+"-"+receiveInfo.getCertificate();
				phoneAddressMap.put(phoneAddress, phoneAddressMap.get(phoneAddress)==null?1:(phoneAddressMap.get(phoneAddress)+1));
				phoneCertificateMap.put(phoneCertificate, phoneCertificateMap.get(phoneCertificate)==null?1:(phoneCertificateMap.get(phoneCertificate)+1));
				
				if(phoneAddress.equals(currentPhoneAddress)){
					//存在与当前用户同手机号-地址，且同身份证的
					if(receiveInfo.getCertificate().equals(riskOrdersInfo.getCertificate())){
						currentPhoneCertificateflag=true;
					}
				}
				
				if(phoneCertificate.equals(currentPhoneCertificate)){
					//存在与当前用户同手机号-身份证，且同地址的
					if(receiveInfo.getAddress().equals(riskOrdersInfo.getAddress())){
						currentPhoneAddressflag=true;
					}
				}
				
			}
			
			//判断加入当前身份证信息
			if(!currentPhoneCertificateflag){
				if(phoneAddressMap.get(currentPhoneAddress)==null){
					phoneAddressMap.put(currentPhoneAddress, 1);
				}else{
					phoneAddressMap.put(currentPhoneAddress, phoneAddressMap.get(currentPhoneAddress)+1);
				}
			}
			
			//判断加入当前地址信息
			if(!currentPhoneAddressflag){
				if(phoneCertificateMap.get(currentPhoneCertificate)==null){
					phoneCertificateMap.put(currentPhoneCertificate, 1);
				}else{
					phoneCertificateMap.put(currentPhoneCertificate, phoneCertificateMap.get(currentPhoneCertificate)+1);
				}
			}
			//同一手机号，同一收货地址，大于等于3个收货人---304
			if(!phoneAddressMap.isEmpty()&&phoneAddressMap.get(currentPhoneAddress)>=3){
				riskRank=BaseConsts.RISKRANK_HIGH;
				detailInfo.put("riskRank", riskRank);
				riskItemSb.append(BaseConsts.RISKITEM_304).append(",");
			}
			
			//同一手机号，大于等于3个收货地址---305
			if(!phoneAddressMap.isEmpty()&&phoneAddressMap.size()>=3){
				riskRank=BaseConsts.RISKRANK_HIGH;
				detailInfo.put("riskRank", riskRank);
				riskItemSb.append(BaseConsts.RISKITEM_305).append(",");
			}
			
			//同一手机号，大于等于3个收货人---306
			if(!phoneCertificateMap.isEmpty()&&phoneCertificateMap.size()>=3){
				riskRank=BaseConsts.RISKRANK_HIGH;
				detailInfo.put("riskRank", riskRank);
				riskItemSb.append(BaseConsts.RISKITEM_306).append(",");
			}
			
		}
		
		if(StringUtils.isNotBlank(riskItemSb.toString())){
			detailInfo.put("riskItem", riskItemSb.toString());
		}
		responseInfoVO.setDetailInfo(detailInfo);
		return responseInfoVO;
	}

	
	@Transactional
	@Override
	public void updateUserAmount(RiskOrdersInfo riskOrdersInfo) throws Exception {
		if(riskOrdersInfo==null || StringUtils.isEmpty(riskOrdersInfo.getCertificate()) ||
				StringUtils.isEmpty(riskOrdersInfo.getBuyerPhone()) || StringUtils.isEmpty(riskOrdersInfo.getAddress())){
			throw new Exception("parameter error");
		}
		
		int currentYear = Calendar.getInstance().get(Calendar.YEAR);
		//根据身份证更新额度
		CertificateRestrict certificateRestrict = new CertificateRestrict();
		certificateRestrict.setCertificate(riskOrdersInfo.getCertificate());
		certificateRestrict.setAmount(riskOrdersInfo.getOrdersTotal());
		certificateRestrict.setCurrentYear(currentYear);
		receiveInfoDao.updateAmountByCertificate(certificateRestrict);
		
		//根据电话更新额度
		PhoneRestrict phoneRestrict = new PhoneRestrict();
		phoneRestrict.setPhone(riskOrdersInfo.getBuyerPhone());
		phoneRestrict.setAmount(riskOrdersInfo.getOrdersTotal());
		phoneRestrict.setCurrentYear(currentYear);	
		receiveInfoDao.updateAmountByPhone(phoneRestrict);
		
		//根据地址更新额度
		AddressRestrict addressRestrict = new AddressRestrict();
		addressRestrict.setAddress(riskOrdersInfo.getAddress());
		addressRestrict.setAmount(riskOrdersInfo.getOrdersTotal());
		addressRestrict.setCurrentYear(currentYear);
		receiveInfoDao.updateAmountByAddresss(addressRestrict);
		
		return;
	}
}
